﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Web.App_Code;
using QLK.Database;
using QLK.Database.BLLs;

namespace QLK.Web
{
    public partial class ExportInvoiceDetails : System.Web.UI.Page
    {
        string _CustID;
        string _InvoiceID;
        CustomerBLL CustBLL = new CustomerBLL();
        StorageBLL storageBLL = new StorageBLL();
        ExportInvoiceBLL ExpBLL = new ExportInvoiceBLL();
        Invoices_ProductBLL IPBLL = new Invoices_ProductBLL();
        Invoices_Product InvPro;

        protected void Page_Load(object sender, EventArgs e)
        {
            _CustID = Request.QueryString[Columns.CustomerID];
            _InvoiceID = Request.QueryString[Columns.ExportID];

            if (!IsPostBack)
            {
                Load_Data();
            }
        }

        protected void grvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvProducts.PageIndex = e.NewPageIndex;            
            Load_Data();
        }

        private void Load_Data()
        {
            //Load drlStorages
            drlStorages.DataSource = storageBLL.StorageGetAll();
            drlStorages.DataValueField = Columns.StorageID;
            drlStorages.DataTextField = Columns.StorageName;
            drlStorages.DataBind();

            txtInvoiceID.Text = _InvoiceID;
            txtCustID.Text = _CustID;

        }

        protected void drlStorages_SelectedIndexChanged(object sender, EventArgs e)
        {
            int exportID;
            int storageID = int.Parse(drlStorages.SelectedValue.ToString());

            ExportInvoice exp = ExpBLL.ExportInvoiceGetByInvoiceID(txtInvoiceID.Text);

            if (exp == null)
                lblWarning.Text = "No products been added!";
            else
            {
                exportID = exp.ExportID;
                grvProducts.DataSource = ExpBLL.ExportInvoices_GetProductByExportIDandStorageID(exportID, storageID);
                grvProducts.DataBind();
            }
        }

        protected void ImgSearchProduct_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}",
                PageUrls.SearchProducts, "ID", "0", Columns.InvoiceID, txtInvoiceID.Text.ToString()));
        }

        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < grvProducts.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)grvProducts.Rows[i].Cells[0].FindControl("ckChoose");
                if (ck.Checked == true) 
                {
                    int ProID = int.Parse(ck.Attributes["_ProID"]);
                    int InvoiceID = int.Parse(ck.Attributes["_InvID"]);

                    if (IPBLL.Invoices_ProductDeleteProduct(InvoiceID, true, ProID))
                    {
                        lblWarning.Text = "Delete successfully!!";
                        Load_Data();
                    }
                    else
                    {
                        lblWarning.Text = "Delete failed!!";
                    }
                }
            }
        }

        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < grvProducts.Rows.Count; i++)
            {
                TextBox txtquantity = (TextBox)grvProducts.Rows[i].Cells[4].FindControl("txtQuantity");
                CheckBox ck = (CheckBox)grvProducts.Rows[i].Cells[0].FindControl("ckChoose");
                int ProID = int.Parse(ck.Attributes["_ProID"]);
                int InvoiceID = int.Parse(ck.Attributes["_InvID"]);

                InvPro = new Invoices_Product();
                InvPro.InvoiceID = InvoiceID;
                InvPro.ProductID = ProID;
                InvPro.Price = decimal.Parse(grvProducts.Rows[i].Cells[2].Text);
                InvPro.Quantity = int.Parse(txtquantity.Text);
                InvPro.TotalValues = (decimal.Parse(grvProducts.Rows[i].Cells[2].Text)) * (int.Parse(txtquantity.Text));
                InvPro.Type = true;

                if (IPBLL.Invoices_ProductUpdate(InvPro))
                {
                    lblWarning.Text = "Save successfully!";
                }
                else
                    lblWarning.Text = "Save failed!";
            }
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}", PageUrls.ExportInvoices, Columns.InvoiceID, txtInvoiceID.Text,
                Columns.CustomerID, txtCustID.Text));
        }
    }
}
